#!/usr/bin/env python
import os, sys
import healpy as hp
import numpy as np
from argparse import ArgumentParser
from cosat.gif import *
import iniconfig
import matplotlib.pylab as plt

from PIL import Image, ImageDraw
import imageio

def genGIF():
    parser = ArgumentParser()
    parser.add_argument('nside', default=512, type=int)
    parser.add_argument('--band', default='', type=str)
    parser.add_argument('--idx_yrTags', default='yrTags.txt', type=str)
    parser.add_argument('--min', default=1, type=int,
                        help='min visit number')
    parser.add_argument('--max', default=8, type=int,
                        help='max visit number')
    parser.add_argument('--norm', '-n', default='none', type=str, 
                        help='norm can be one of: none, hist, log')
    parser.add_argument('--unit', '-u', default='visits', type=str)
    parser.add_argument('--coord', '-c', nargs='+', default='E', type=str,
                        help='E: Ecliptical; G: Galactic; C: Equatorial')
    parser.add_argument('--cmap', '-m', default='CMRmap', type=str)
    parser.add_argument('--cbar', '-b', default=True, type=bool)
    parser.add_argument('--duration', '-d', default=1000.0, type=float)

    args = parser.parse_args()

    coord = ['E']  # default
    # print('args.coord: {}'.format(args.coord))
    if args.coord[0] == 'C':
        print('> transforming from Ecliptcical to Equatorial')
        coord = ['E', 'C']
    if args.coord[0] == 'G':
        print('> transforming from Ecliptcical to Galactic')
        coord = ['E', 'G']

    plist = makePNG(band = args.band, \
                    nside = args.nside, \
                    idx_yrTags = args.idx_yrTags, \
                    min = args.min, \
                    max = args.max, \
                    norm = args.norm, \
                    unit = args.unit, \
                    # coord = args.coord, \
                    coord=coord, \
                    cmap = args.cmap, \
                    cbar = args.cbar
                    )

    if args.band == '':
        gifName = 'all_bands_NSIED_{}.gif'.format(args.nside)
    else:
        gifName = args.band + '_NSIDE_{}.gif'.format(args.nside)

    makeGIF(plist, gifName, args.duration)


if __name__ == '__main__':
    genGIF()
